package com.dominator.controller.api;

import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.ApiUserService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户相关
 *
 * @author Administrator
 */
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/api/v1/user")
public class ApiUserController {
    @Autowired
    HttpServletRequest request;

    @Autowired
    ApiUserService apiUserService;

    /**
     * 用户昵称或性别修改
     *
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public ApiMessage updateUser(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            msg = apiUserService.updateUser(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }

    }


    /**
     * 更改密码
     *
     * @param
     * @return
     */
    @PostMapping(value = "/modifyPassword")
    public ApiMessage modifyPassword(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiUserService.modifyPassword(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }

    /**
     * 找回（设置）密码
     *
     * @param
     * @return
     */
    @PostMapping(value = "/findPassword")
    public ApiMessage findPassword(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiUserService.findPassword(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }


    /**
     * 获取用户信息
     */
    @RequestMapping("/getUser")
    @ResponseBody
    public ApiMessage getUser(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiUserService.getUser(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }

    /**
     * 绑定微信openId
     */
    @RequestMapping("/bindingWxOpenId")
    @ResponseBody
    public ApiMessage bindingWxOpenId(@RequestParam String paramStr) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(paramStr);
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiUserService.bindingWxOpenId(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            return msg;
        }
    }
}